package com.app.team.liquid;

//============================================================================
//Name        : ThreadsList.java
//Author      : Kyle Bartush
//Version     : 0.1
//Description : Shows a list of threads in a list view, includes the thread name
//				thread category picture (hot, sticky, etc) and author.
//FutureGoals : Implement a selector for showing the user when he clicks an item.
//============================================================================

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.HttpClient;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class ThreadsList extends Activity implements OnItemClickListener {

	private String targetUrl, targetName;
	private List<ForumThread> threads = new ArrayList<ForumThread>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.threads_list);
		getData();

		setTitle(targetName);

		TLParse parser = new TLParse(this.getApplication());
		if (targetUrl.contains("news")) {
			parser.setNews(true);
		}
		parser.setSource(" No Error ");
		try {
			parser.setHtmlByUrl(targetUrl);
		} catch (IOException e) {
			e.printStackTrace();
		}

		parser.parseThreads();

		threads = parser.getThreadsList();

		ForumThreadArrayAdapter adapter = new ForumThreadArrayAdapter(
				getApplicationContext(), R.layout.thread_listitem, threads);

		ListView lv = (ListView) this.findViewById(R.id.threadsLV);

		lv.setAdapter(adapter);

		lv.setOnItemClickListener(this);

	}

	private void getData() {
		Bundle inBundle = getIntent().getExtras();
		targetUrl = inBundle.getString("url");
		targetName = inBundle.getString("name");
	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

		ForumThread thread = (ForumThread) arg0.getItemAtPosition(arg2);
		Bundle urlBun = new Bundle();
		urlBun.putString("url", "http://www.teamliquid.net" + thread.url);
		urlBun.putString("title", thread.title);
		urlBun.putString("author", thread.author);
		urlBun.putString("page", "1");
		Intent i = new Intent(ThreadsList.this, ThreadView.class);
		i.putExtras(urlBun);
		startActivity(i);
	}
}
